import { defineStore } from "pinia";
import { AuthState } from "@/stores/interface";
import { getAuthMenuListApi } from "@/api/modules/login";
import { getFlatMenuList, getShowMenuList, getAllBreadcrumbList } from "@/utils";
import { useFondsStore } from "@/stores/modules/fonds";
import { useGlobalStore } from "@/stores/modules/global";
import { transTree, processFMMenuTree } from "@/utils/util";
import { cloneDeep } from "lodash-es";

export const useAuthStore = defineStore({
  id: "geeker-auth",
  state: (): AuthState => ({
    // 按钮权限列表
    authButtonList: {},
    // 菜单权限列表
    authMenuList: [],
    // 新的菜单列表
    newAuthMenuList: [],
    // 当前页面的 router name，用来做按钮权限筛选
    routeName: ""
  }),
  getters: {
    // 按钮权限列表
    authButtonListGet: state => state.authButtonList,
    // 菜单权限列表 ==> 这里的菜单没有经过任何处理
    authMenuListGet: state => state.authMenuList,
    // 菜单权限列表 ==> 左侧菜单栏渲染，需要剔除 isHide == true
    showMenuListGet: state => getShowMenuList(state.authMenuList),
    // 菜单权限列表 ==> 扁平化之后的一维数组菜单，主要用来添加动态路由
    flatMenuListGet: state => getFlatMenuList(state.authMenuList) as any,
    // 新的菜单权限列表
    newFlatMenuListGet: state => getFlatMenuList(state.newAuthMenuList) as any,
    // 递归处理后的所有面包屑导航列表
    breadcrumbListGet: state => getAllBreadcrumbList(state.newAuthMenuList)
  },
  actions: {
    // Get AuthMenuList
    async getAuthMenuList() {
      const fondsStore = useFondsStore();
      const globalStore = useGlobalStore();
      const { List } = await getAuthMenuListApi();
      const newList = cloneDeep(List);
      const fondsData = JSON.stringify(fondsStore.fondsData.length > 0 ? fondsStore.fondsData : fondsStore.getFondsData);
      const fondsDataParse = JSON.parse(fondsData);
      fondsDataParse.forEach((item: any) => {
        item.MNU_ID = item.ID;
        item.MNU_Title = item.Name;
        // item.name = "MainModal";
        item.path = "/system/dynamicMain/" + item.ID;
        item.meta = {
          icon: "icon-file",
          title: item.Name,
          isLink: "",
          isHide: false,
          isFull: false,
          isAffix: false,
          isKeepAlive: true
        };
        if (item.Type === "Fonds") {
          item.meta.icon = "icon-quanzonghaobiao";
        }
        if (item.Type === "FM") {
          item.meta.icon = "icon-fenlei-copy";
        }
        if (item.Type === "FMT") {
          item.meta.icon = "icon-menlei";
        }
      });
      const filterData = fondsDataParse.filter((v: any) => v.Type === "FMT");
      const resultData = fondsDataParse.filter((v: any) => !filterData.some((j: any) => v.ID === j.ID && j.FMT_Parent_ID));
      const newFondsData = transTree(resultData, "ID", "FatherID");
      const newMenuList = transTree(cloneDeep(fondsDataParse), "ID", "FatherID");
      // 将菜单中重复的数据去除
      for (let i = 0; i < newFondsData.length; i++) {
        const tempArr1 = newFondsData[i];
        for (let j = 0; j < List.length; j++) {
          const tempArr2 = List[j];
          if (tempArr1.Name === tempArr2.MNU_Title) {
            if (tempArr1.children) {
              tempArr2.children.push(...tempArr1.children);
            }
          }
        }
      }
      // 将新菜单中重复的数据去除
      for (let i = 0; i < newMenuList.length; i++) {
        const tempArr1 = newMenuList[i];
        for (let j = 0; j < newList.length; j++) {
          const tempArr2 = newList[j];
          if (tempArr1.Name === tempArr2.MNU_Title) {
            if (tempArr1.children) {
              tempArr2.children.push(...tempArr1.children);
            }
          }
        }
      }
      // 去除掉全宗子级为空的数据
      List.forEach((item: any, index: number) => {
        if (item.path === "Collect" || item.path === "Arrangement" || item.path === "Storage" || item.path === "Utilization") {
          if (item.children.length === 0) {
            List.splice(index, 1);
          }
        }
      });
      this.authMenuList = processFMMenuTree(List);
      this.newAuthMenuList = newList;
    },
    // Set RouteName
    async setRouteName(name: string) {
      this.routeName = name;
    }
  }
});
